﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_HRM_WF_Finances : IDAL_HRM_WF_Finances
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_HRM_WF_Finances()
        {

        }

        public IList<M_HRM_WF_Finances> GetFull()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_WF_Finances> infoList = new List<M_HRM_WF_Finances>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT f.finance_seq, f.finance_name, f.contact_seq, f.remarks"
                     + ", c.contact_name"

                     + ", f.record_status, f.created_by, f.created_when, f.updated_by, f.updated_when"
                     + " FROM hrm_wf_finances f"
                     + " LEFT JOIN fim_mas_contacts c ON f.contact_seq = c.contact_seq"
                    + " WHERE f.record_status not like @delete_status";

                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_WF_Finances info = new M_HRM_WF_Finances();
                        info.finance_seq = Convert.ToInt32(drc[i]["finance_seq"]);
                        info.finance_name = drc[i]["finance_name"].ToString();
                        info.contact_seq = cn.ConvertNullInt(drc[i]["contact_seq"]);
                        info.remarks = drc[i]["remarks"].ToString();

                        info.contact_name = drc[i]["contact_name"].ToString();

                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_HRM_WF_Finances GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_HRM_WF_Finances info = new M_HRM_WF_Finances();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT f.finance_seq, f.finance_name, f.contact_seq, f.remarks"
                     + ", c.contact_name"

                     + ", f.record_status, f.created_by, f.created_when, f.updated_by, f.updated_when"
                     + " FROM hrm_wf_finances f"
                     + " LEFT JOIN fim_mas_contacts c ON f.contact_seq = c.contact_seq"
                    + " where f.finance_seq = @seq";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.finance_seq = Convert.ToInt32(drc[0]["finance_seq"]);
                    info.finance_name = drc[0]["finance_name"].ToString();
                    info.contact_seq = cn.ConvertNullInt(drc[0]["contact_seq"]);
                    info.remarks = drc[0]["remarks"].ToString();

                    info.contact_name = drc[0]["contact_name"].ToString();

                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Update(M_HRM_WF_Finances info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@finance_seq", info.finance_seq);
                dbManager.AddParameters(1, "@contact_seq", info.contact_seq);

                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_wf_finances"
                    + " set contact_seq = @contact_seq"

                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE finance_seq = @finance_seq";
                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(string strFinanceSeq)
        {
            string strSQL;

            try
            {
                dbManager.Open();

                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@finance_seq", strFinanceSeq);
                dbManager.AddParameters(1, "@status", SystemInfo._DeleteRecordStatus);

                strSQL = @"UPDATE hrm_wf_finances 
                          SET record_status = @status 
                          WHERE finance_seq = @finance_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
